package com.zhanglijie.improve.collections.delayqueue.demo;

import java.util.Locale;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import com.zhanglijie.improve.aqs.exclusive.MyReentrantLock;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/12/4 0004 18:50
 */
public class InterruptDemo {
    private static final transient MyReentrantLock lock = new MyReentrantLock();
    private static final Condition available = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(()->{
            try {
                lock.lockInterruptibly();
                System.out.println("t1 exe ...");
                available.await();
                System.out.println("t1被中断后正常继续执行");
            } catch (InterruptedException e) {
                System.out.println("t1 interrupt");
            }finally {
                System.out.println("t1 exit ");
                lock.unlock();
            }
        },"t1");

        t1.start();

        new Thread(()->{
            try {
                lock.lock();
                System.out.println("t3 exe..");
                Thread.sleep(1000);
                t1.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                System.out.println("t3 exit");
                lock.unlock();
            }
        },"t3").start();




        System.out.println("=================================================================================================");





        Thread t2= new Thread(()->{
            try {
                lock.lockInterruptibly();
                System.out.println("t2 exe...");
                long leftTime = available.awaitNanos(8804546556544866846L);
                System.out.println("t2(带参数时间）被中断后正常继续执行"+leftTime);
            } catch (InterruptedException e) {
                System.out.println("t2(带参数时间） interrupt");
            }finally {
                System.out.println("t2 exit");
                lock.unlock();
            }
        },"t2");
        t2.start();
        new Thread(()->{
            try {
                lock.lock();
                System.out.println("t4 exe...");
                Thread.sleep(1000);
                available.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                System.out.println("t4 exit");
                lock.unlock();
            }
        },"t4").start();

    }
}
